package com.ganbo.thread.atomic;

import java.util.concurrent.atomic.AtomicLong;

class MyThread extends Thread {

    private MyService myService;

    public MyThread(MyService myService) {
        this.myService = myService;
    }

    @Override
    public void run() {
        myService.addNumber();
    }
}

public class MyService {

    public static AtomicLong al = new AtomicLong();

    public void addNumber() {
        synchronized (MyService.class) {
            System.out.println(Thread.currentThread().getName() + "加100之后的值是:" + al.addAndGet(100));
            al.addAndGet(1);
        }
    }

    public static void main(String[] args) {
        MyService myService = new MyService();

        MyThread[] myThreads = new MyThread[5];
        for (int i = 0; i < myThreads.length; i++) {
            myThreads[i] = new MyThread(myService);
        }

        for (int i = 0; i < myThreads.length; i++) {
            myThreads[i].start();
        }

    }
}

